package com.xdy.server.core2;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.util.Date;

/**
 * @Author: XDY.
 * @Email: 2280944114@qq.com
 * @CreateTime: 2023-03-13  16:55
 * @Description: 封装响应
 * @Version: 1.0
 */
public class Response {

    //响应空格
    private static final String BLANK = " ";

    //响应空行
    private static final String CRLF = "\r\n";

    private SocketChannel socketChannel;

    private ByteBuffer byteBuffer;

    //正文
    private StringBuilder content;

    //正文长度字节数
    private int length;

    //响应头
    private StringBuilder headInfo;

    private StringBuilder addHead;

    private String contentType = "text/html;charset=UTF-8";

    private Response() {
        content = new StringBuilder();
        headInfo = new StringBuilder();
        addHead = new StringBuilder();
        length = 0;
    }

    public Response(SocketChannel socketChannel) {
        this();
        this.socketChannel = socketChannel;
    }

    public Response print(String info) {
        content.append(info);
        length += info.getBytes(StandardCharsets.UTF_8).length;
        return this;
    }

    public Response println(String info) {
        content.append(info).append(CRLF);
        length += (info + CRLF).getBytes(StandardCharsets.UTF_8).length;
        return this;
    }

    public void write(byte[] data) throws IOException {
        length = data.length;
        buildHeadInfo(200);
        ByteBuffer headBuffer = ByteBuffer.wrap(headInfo.toString().getBytes(StandardCharsets.UTF_8));
        ByteBuffer byteBuffer = ByteBuffer.wrap(data);
        ByteBuffer[] responseBuffer = {headBuffer, byteBuffer};
        socketChannel.write(responseBuffer);
    }

    public void pushToBrowser(int code) throws IOException {
        if (headInfo == null) {
            code = 505;
        }
        buildHeadInfo(code);
        System.out.println("headInfo===>" + headInfo);
        byteBuffer = ByteBuffer.wrap((headInfo.toString() + content.toString()).getBytes(StandardCharsets.UTF_8));
        socketChannel.write(byteBuffer);
    }

    /**
     * 构建响应头信息
     *
     * @param code
     */
    private void buildHeadInfo(int code) {
        //响应行
        headInfo.append("HTTP/1.1").append(BLANK).append(code).append(BLANK);
        switch (code) {
            case 200:
                headInfo.append("OK").append(CRLF);
                break;
            case 404:
                headInfo.append("NOT FOUND").append(CRLF);
                break;
            case 403:
                headInfo.append("FORBIDDEN").append(CRLF);
                break;
            case 505:
                headInfo.append("SERVER ERROR").append(CRLF);
                break;
        }
        //响应头
        headInfo.append("Date: ").append(new Date()).append(CRLF);
        headInfo.append("Server: ").append("DyServer/0.0.1;charset=UTF-8").append(CRLF);
        headInfo.append("Content-Type: ").append(contentType).append(CRLF);
        headInfo.append("Content-Length: ").append(length).append(CRLF);
        headInfo.append(addHead.toString()).append(CRLF);
    }

    /**
     * 设置响应内容
     *
     * @param contentType
     */
    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    /**
     * 添加头信息
     *
     * @param head
     * @param value
     */
    public void addHead(String head, String value) {
        addHead.append(head + ": ").append(value).append(CRLF);
    }
}
